package com.lenovo.smarttraffic.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.api.ITitle;
import com.lenovo.smarttraffic.interfaceImpl.JsToJavaImpl;
/**
 * Created by Caleb.F
 * on 2020-06-04
 * at 15:50
 */
@SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
public class UserWebActivity extends BaseActivity {
    private WebView mMyWebView;
    private ProgressBar mMyProgress;
    private static final String LOCAL_HTML = "file:///android_asset/www/index.html";
    private static final String TAG = "UserWebActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        initToolBar("用户签到");
        mMyWebView = findViewById(R.id.my_web_view);
        mMyProgress = findViewById(R.id.my_progress);
        initWebView();
    }


    private void initWebView() {
        WebSettings settings = mMyWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        mMyWebView.setWebViewClient(new WebViewClient());
        setProgress();
        mMyWebView.addJavascriptInterface(new JsToJavaImpl(UserWebActivity.this
                , new ITitle() {
            @Override
            public void setTitle(String title) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initToolBar(title);
                        //展示精度条
                      setProgress();
                    }
                });
            }
        }), "Java");
        mMyWebView.loadUrl(LOCAL_HTML);
    }

    private void setProgress() {
        mMyWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                mMyProgress.setVisibility(View.VISIBLE);
                mMyProgress.setProgress(newProgress);
                //加载本地资源，速度太快
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                    if (100 == newProgress) {
                        mMyProgress.setVisibility(View.GONE);
                    }
                }
            }, 1000);
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_user_web;
    }


    /**
     * 处理按键返回按键返回事件
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(KeyEvent.KEYCODE_BACK==keyCode){
            if (mMyWebView.canGoBack()) {
                mMyWebView.goBack();
                initToolBar("用户中心");
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressedSupport() {
        if (mMyWebView.canGoBack()) {
            mMyWebView.goBack();
            initToolBar("用户中心");
        }
        else{
           super.onBackPressedSupport();
        }
    }


}
